package au.edu.swin.videoshop.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Title implements Serializable{
	private Long mTitleId;
	private String mTitleName;
	private String mDescription;
	private TitleCategory mCategory;
	private Classification mClassification;
	private int mYear;
	private List mMedia;
	
	public Title() {
	}
	
	
	/**
	 * @return the titleId
	 */
	public Long getTitleId() {
		return mTitleId;
	}

	/**
	 * @param titleId the titleId to set
	 */
	public void setTitleId(Long titleId) {
		mTitleId = titleId;
	}

	/**
	 * @return the titleName
	 */
	public String getTitleName() {
		return mTitleName;
	}
	
	/**
	 * @param titleName the titleName to set
	 */
	public void setTitleName(String titleName) {
		mTitleName = titleName;
	}
	
	/**
	 * @return the description
	 */
	public String getDescription() {
		return mDescription;
	}
	
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		mDescription = description;
	}
	
	/**
	 * @return the category
	 */
	public TitleCategory getCategory() {
		return mCategory;
	}
	
	/**
	 * @param category the category to set
	 */
	public void setCategory(TitleCategory category) {
		mCategory = category;
	}
	
	/**
	 * @return the classification
	 */
	public Classification getClassification() {
		return mClassification;
	}

	/**
	 * @param classification the classification to set
	 */
	public void setClassification(Classification classification) {
		mClassification = classification;
	}

	/**
	 * @return the year
	 */
	public int getYear() {
		return mYear;
	}

	/**
	 * @param year the year to set
	 */
	public void setYear(int year) {
		mYear = year;
	}
	
	public String getShortDescription() {
		if (mDescription != null) {
			if (mDescription.length() > 200) {
				return mDescription.substring(0, 200);
			}
			else {
				return mDescription;
			}
		}
		
		return null;
	}
	
	public String getIconUrl() {
		if (mTitleName != null) {
			String imageName = mTitleName.replace(' ', '_').toLowerCase();
			return "/images/titles/" + imageName + ".jpg";
		}
		else {
			return "/images/titles/_no_title.jpg";
		}
	}
	

	/**
	 * @return the media
	 */
	public List getMedia() {
		return mMedia;
	}
	
	/**
	 * @return the media
	 */
	public List getAvailableMedia() {
		List retVal = new ArrayList();
		if (mMedia != null) {
			for (Iterator i = mMedia.iterator(); i.hasNext();) {
				TitleMedium medium = (TitleMedium) i.next();
				
				if (medium.isAvailable()) {
					retVal.add(medium);
				}
			}
		}
		
		return retVal;
	}

	/**
	 * @param media the media to set
	 */
	public void setMedia(List media) {
		mMedia = media;
	}
	
	public TitleMedium addMedium(MediumType mediumType) {
		if (mMedia == null) {
			mMedia = new ArrayList();
		}
		TitleMedium medium = new TitleMedium();
		medium.setTitle(this);
		medium.setMediumType(mediumType);
		
		mMedia.add(medium);
		
		return medium;
	}
	

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj instanceof Title) {
			return this.mTitleId.equals(((Title)obj).mTitleId);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return mTitleId.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return mTitleName;
	}
	
	public static class YearComparator implements Comparator{

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2) {
			return ((Title)o1).getYear() - ((Title)o2).getYear();
		}
	}
	
	public static class TitleNameComparator implements Comparator{

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2) {
			return ((Title)o1).getTitleName().compareTo(((Title)o2).getTitleName());
		}
	}
	
	public static class ClassificationComparator implements Comparator{

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2) {
			return ((Title)o1).getClassification().compareTo(((Title)o2).getClassification());
		}
	}
}
